﻿var date_format_string = 'm/Y';
var date_sql_format_string = 'Y-m-d';
var page_size = 50;

function render_number(val){
    try {
        if (val == null || val == '') 
            val = 0.00;
        return number_format_extra(val, decimals, decimalSeparator, thousandSeparator);
    } 
    catch (e) {
    }
};

function cN(val){
    if (isEmpty(val)) {
        return 0;
    }
    if (is_string(val)) {
        if (val.indexOf(thousandSeparator) > 0) {
            val = val.replaceAll(thousandSeparator, decimalSeparator);
        }
        val = formatNumber(val);
    }
    return val;
};

Ext.onReady(function(){
    Ext.QuickTips.init();
    
    var pathRequestUrl = 'CrossCheckInformation_Data.php';
    
    var dsCode = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: pathRequestUrl + '/getSearchProduct/1'
        }),
        reader: new Ext.data.JsonReader({
            root: 'data',
            totalProperty: 'count',
            id: 'product_id'
        }, [{
            name: 'product_id',
            type: 'string'
        }, {
            name: 'product_name',
            type: 'string'
        }, {
            name: 'product_code',
            type: 'string'
        }]),
        autoLoad: false
    });
    dsCode.on('beforeload', function(){
        if (Ext.getCmp('searchProductCode').hasFocus) {
            dsCode.baseParams.type = 'code';
        }
        else {
            dsCode.baseParams.type = 'name';
        }
    });
    var resultTplCode = new Ext.XTemplate('<tpl for="."><div class="search-item">', '<span>{product_code} | {product_name}</span><br />', '</div></tpl>');
    var productCode = new Ext.form.ComboBox({
        id: 'searchProductCode',
        store: dsCode,
        valueField: 'product_id',
        typeAhead: false,
        pageSize: 50,
        triggerAction: 'all',
        //hideTrigger:true,
        tpl: resultTplCode,
        width: 70,
        lazyRender: true,
        listWidth: 350,
        selectOnFocus: true,
        minChars: 1,
        itemSelector: 'div.search-item',
        enableKeyEvents: true
    });
    
    var productName = new Ext.form.ComboBox({
        store: dsCode,
        id: 'searchProductName',
        valueField: 'product_id',
        typeAhead: false,
        pageSize: 50,
        triggerAction: 'all',
        //hideTrigger:true,
        tpl: resultTplCode,
        width: 120,
        lazyRender: true,
        listWidth: 350,
        selectOnFocus: true,
        minChars: 1,
        itemSelector: 'div.search-item',
        enableKeyEvents: true
    });
    
    var productRecord = [{
        name: 'id',
        type: 'string'
    }];
    
    var product_object = Ext.data.Record.create(productRecord);
    
    var Product = {
        newRow: function(){
        
        },
        addNew: function(){
        
        },
        find: function(){
        
        },
        remove: function(){
        
        }
    };
    
    var tbarProduct = new Ext.Toolbar({
        items: [{
            xtype: 'label',
            text: 'Đối tác',
            style: 'padding-left: 5px; padding-right: 5px;'
        }, new Ext.form.ComboBox({
            id: 'cboPartner',
            store: null,
            forceSelection: true,
            displayField: 'partner_name',
            valueField: 'partner_id',
            typeAhead: false,
            triggerAction: 'all',
            mode: 'local',
            //tpl: resultTplSubjectCode,
            forceSelection: true,
            triggerAction: 'all',
            allowBlank: true,
            editable: true,
            width: 80,
            minListWidth: 100,
            itemSelector: 'div.search-item',
            minChars: 1,
            listWidth: 350,
            lazyRender: true,
            selectOnFocus: true,
        }), '-', {
            xtype: 'checkbox',
            height: 18,
            width: 20,
            boxLabel: '',
            id: 'chk_month',
            listeners: {
                check: function(it, e){
                }
            }
        }, {
            xtype: 'label',
            text: 'Theo tháng',
            style: 'padding-left: 5px; padding-right: 5px;'
        }, {
            xtype: 'datefield',
            width: 100,
            id: '',
            format: date_format_string,
            allowBlank: false,
            selectOnFocus: true,
            listeners: {
                change: function(e){
                
                }
            }
        }, '-', {
            xtype: 'checkbox',
            height: 18,
            width: 20,
            boxLabel: '',
            id: 'chk_quater',
            listeners: {
                check: function(it, e){
                }
            }
        }, {
            xtype: 'label',
            text: 'Theo quý',
            style: 'padding-left: 5px; padding-right: 5px;'
        }, {
            xtype: 'spinnerfield',
            name: 'quater',
            id: 'quater',
            width: 50,
            length: 4,
            minValue: 1,
            maxValue: 4,
            allowDecimals: false,
            incrementValue: 1,
            accelerate: true,
            value: 1
        }, {
            xtype: 'label',
            text: 'năm',
            style: 'padding-left: 5px; padding-right: 5px;'
        }, {
            xtype: 'spinnerfield',
            name: 'year',
            id: 'year',
            width: 50,
            length: 4,
            minValue: 2010,
            maxValue: 2050,
            allowDecimals: false,
            incrementValue: 1,
            accelerate: true,
            value: 2010
        }, '-', {
            xtype: 'label',
            text: 'Loại quyền',
            style: 'padding-left: 5px; padding-right: 5px;'
        }, new Ext.form.ComboBox({
            id: 'cboPermission',
            store: null,
            forceSelection: true,
            displayField: 'partner_name',
            valueField: 'partner_id',
            typeAhead: false,
            triggerAction: 'all',
            mode: 'local',
            //tpl: resultTplSubjectCode,
            forceSelection: true,
            triggerAction: 'all',
            allowBlank: true,
            editable: true,
            width: 100,
            minListWidth: 100,
            itemSelector: 'div.search-item',
            minChars: 1,
            listWidth: 350,
            lazyRender: true,
            selectOnFocus: true,
        }), '->', {
            text: 'In',
            id: 'printer_add',
            cls: 'x-btn-text-icon',
            icon: 'images/icons/printer.png'
        }, '-', {
            text: 'Tải lại',
            id: 'reload_detail',
            iconCls: 'btn-text-icon',
            icon: './images/icons/refresh.png',
            handler: Product.find
        }]
    });
    
    var cmbPerPage = new Ext.form.ComboBox({
        name: 'perpage',
        width: 80,
        store: new Ext.data.SimpleStore({
            data: [[50, '50'], [100, '100'], [150, '150'], [200, '200'], [250, '250'], [300, '300'], [500, '500'], [1000, '1000']],
            id: 0,
            fields: ['id', 'value']
        }),
        mode: 'local',
        value: '50',
        listWidth: 80,
        triggerAction: 'all',
        displayField: 'value',
        valueField: 'value',
        editable: false,
        forceSelection: true
    });
    
    cmbPerPage.on('select', function(combo, record){
        bbarProduct.pageSize = parseInt(record.get('id'), 10);
        bbarProduct.doLoad(bbarProduct.cursor);
    }, this);
    
    var bbarProduct = new Ext.PagingToolbar({
        store: storeProduct, //the store you use in your grid
        displayInfo: true,
        pageSize: page_size,
        items: ['-', 'Số dòng/trang', cmbPerPage]
    });
    
    var cmProduct = new Ext.grid.ColumnModel({
        defaults: {
            sortable: false
        },
        columns: [new Ext.grid.RowNumberer(), {
            header: 'Đối tác',
            editable: true
        }, {
            header: 'Mã bài hát',
            editable: true
        }, {
            header: 'Tên bài hát',
            editable: true
        }, {
            header: 'Ca sĩ',
            editable: true
        }, {
            header: 'Nhạc sĩ',
            editable: true
        }, {
            header: 'Quyền',
            editable: true
        }, {
            header: 'Số lượng',
            editable: true
        }, {
            header: 'Giá',
            editable: true
        }, {
            header: 'Tổng tiền',
            editable: true
        }],
        editors: {},
        getCellEditor: function(colIndex, rowIndex){
        
        }
    });
    
    var storeProduct = new Ext.data.Store({
        reader: new Ext.data.JsonReader({
            root: 'data',
            totalProperty: 'count'
        }, product_object),
        proxy: new Ext.data.HttpProxy({
            url: pathRequestUrl + '/getProductBalance/1'
        }),
        autoLoad: false
    });
    
    storeProduct.on('beforeload', function(){
    
    });
    
    storeProduct.on('load', function(){
        Product.addNew();
    });
    
    // create the Grid
    var gridProduct = new Ext.grid.EditorGridPanel({
        title: '',
        store: storeProduct,
        cm: cmProduct,
        stripeRows: true,
        height: 550,
        loadMask: true,
        trackMouseOver: true,
        frame: true,
        clicksToEdit: 1,
        sm: new Ext.grid.RowSelectionModel({
            singleSelect: false
        }),
        tbar: tbarProduct,
        bbar: bbarProduct,
        id: 'gridProduct',
        viewConfig: {
            forceFit: true,
            deferEmptyText: true,
            emptyText: 'No records found'
        },
        listeners: {
            beforeedit: function(e){
            
            },
            afteredit: function(e){
            
            },
            cellclick: function(gridSpec, rowIndex, columnIndex, e){
            
            }
        }
    });
    
    gridProduct.getView().getRowClass = function(record, index){
        if (record.data.isRowNew) 
            return 'format-row-new';
    };
    
    var productBalanceTable = new Ext.FormPanel({
        frame: true,
        title: 'Thông kê thông tin đối soát theo tháng, quý',
        width: 850,
        renderTo: 'CrossCheckTables',
        iconCls: 'form-icon',
        items: [gridProduct]
    });
    
});
